package com.dhcc.core.modules.system.controller;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.constant.state.ResourceGroup;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.modules.system.entity.Shortcut;
import com.dhcc.core.modules.system.service.IShortcutService;


/**
 * 快捷方式表
 * 
 * @ClassName: ShortcutController
 * @Description: TODO
 * @author lf
 * @date 2018-09-26 17:24:23
 */
@Controller
@RequestMapping("${sys.backendPath}/shortcut")
public class ShortcutController extends BaseController {

	private String PREFIX = "/backend/system/shortcut/";
	
	private static Integer RESOURCE_GROUP = ResourceGroup.BACKEND.getCode();

	@Autowired
	private IShortcutService shortcutService;
	

    /**
     * 跳转到快捷方式表列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "shortcut.html";
    }
	
    /**
     * 跳转到添加快捷方式页面
     */
    @RequestMapping(value = "/to_add/{shortcut}")
    public String shortcutAdd(@PathVariable int shortcut, Model model) {
    	model.addAttribute("shortcut",shortcut);
        return PREFIX + "shortcut_add.html";
    }
	
    /**
     * 获取快捷方式表列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<Shortcut> page = new PageFactory<Shortcut>().defaultPage();
        List<Shortcut> result = this.shortcutService.list(page,params);
        page.setRecords((List<Shortcut>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 删除快捷方式表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除快捷方式表")
    public Object delete(@RequestParam Long shortcutId) {
	
        this.shortcutService.deleteById(shortcutId);

        return R.ok();
    }

    /**
     * 获取角色资源列表
     */
    @RequestMapping(value = "/resourceTreeListByRoleId/{createby}")
    @ResponseBody
    public List<ZTreeNode> resourceTreeListByRoleId(@PathVariable Long createby) {
        return this.shortcutService.resourceTreeListByRoleId(RESOURCE_GROUP,createby);
    }
    
    /**
     * 配置快捷方式
     */
    @RequestMapping("/set_shortcut")
    @ResponseBody
    public R setAuthority(@RequestParam("ids") String ids,@RequestParam("createby") Long createBy) {
        this.shortcutService.setShortcut(ids,createBy);
        return R.ok();
    }
    
    /**
     * index页底端快捷方式栏
     */
    @RequestMapping(value = "/indexshortcuts")
    @ResponseBody
    public R indexShortcuts(@RequestParam Long userId) {  
        //将快捷方式传递给主页面
        List<Shortcut> shortcuts=shortcutService.searchByUserId(userId);
        return R.ok().put("shortcuts", shortcuts);
    }
}
